<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;

class Feedback extends Model
{
    use HasFactory;
    use SoftDeletes;

    protected static $unguarded = true;

    protected $table = 'feedback';

    /**
     * 根据ID获取数据
     *
     * @param $id
     * @param mixed $columns
     * @return array|Builder
     */
    public static function getDataByID($id, string $columns = ''){
        $data = self::query();
        if($columns){
            $data->find($id,$columns);
            return $data;
        }
        $val = $data->find($id);
        $status_show =  getStatusValue($val['status']);

        $image = $val['image'];
        $image_path = '';
        $image_show = '';
        if($image){
            $image_path =getImageUrl($image);
            $image_show = '<img width="60" height="60" src="'.$image_path.'">';
        }

        return [
            'id'=>$val['id'],
            'user_id'=>$val['user_id'],
            'username'=>$val['username'],
            'status'=>$val['status'],
            'content'=>$val['content'],
            'image'=>$val['image'],
            'image_path'=>$image_path,
            'image_show'=>$image_show,
            'sort'=>$val['sort'],
            'status_show'=>$status_show,
//            'status_html_show'=>$status_html_show,
            'crated_at'=>Carbon::parse($val['crated_at'])->toDateTimeString(),
            'updated_at'=>Carbon::parse($val['updated_at'])->toDateTimeString(),
        ];
    }

    public function user(){
        return $this->hasOne(Users::class,'id','user_id');
    }
}
